import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-auto-audit-button',
  templateUrl: './auto-audit-button.component.html',
  styleUrls: ['./auto-audit-button.component.scss']
})
export class AutoAuditButtonComponent implements OnInit {
  state: string;
  constructor(public http: HttpClient) { }
  /**
   * @description 获取当前的审方状态
   */
  getState() {
    this.http.get(window.API_ROOT + 'api/recipes/auto_check').subscribe((res: any) => {
      switch (res.data.status) {
        case 'is_running':
          this.state = 'running';
          break;
        case 'is_stopped':
          this.state = 'stopped';
          break;
        default:
          this.state = 'stopped';
          break;
      }
    });
  }
  /**
   * @description 改变自动审方状态
   * @param action 动作
   */
  changeState(action: string) {
    const params = {
      action
    };
    this.http.post(window.API_ROOT + 'api/recipes/auto_check', params).subscribe((res: any) => {
      switch (res.data.status) {
        case 'is_running':
          this.state = 'running';
          break;
        case 'is_stopped':
          this.state = 'stopped';
          break;
        default:
          this.state = 'stopped';
          break;
      }
    });
  }
  ngOnInit() {
    this.getState();
  }

}
